from invenio.bibconvert_python_engine import Record, Controlfield, Field, Subfield
from invenio.config import CFG_SITE_URL
import time, os

def convert(data):
	rec = Record()
	
	rec.add_field(Controlfield("001", data.get("SN", "")))
	rec.add_field(Controlfield("005", time.strftime("%Y%m%d%H%M%S")))
	rec.add_field(Controlfield("008", "%sn||||||||xx#||||#|||z|#||||||%s||" % (time.strftime("%y%m%d"), data.get("lang", "||"))))
	
	lang = Field("041", "0", "7")
	lang.add_subfield(Subfield("a", data.get("lang", "")))
	lang.add_subfield(Subfield("b", data.get("lang2", "")))
	rec.add_field(lang)
	
	dates = Field("046")
	dates.add_subfield(Subfield("k", data.get("dateCreated", "").replace("-", " ")))
	dates.add_subfield(Subfield("j", data.get("dateModified", "").replace("-", " ")))
	rec.add_field(dates)
	
	title = Field("245", "0", "0")
	title.add_subfield(Subfield("a", data.get("titleOrig", "")))
	title.add_subfield(Subfield("b", data.get("titleTrans", "")))
	rec.add_field(title)
	
	altTitle = Field("246", "3", "3")
	altTitle.add_subfield(Subfield("a", data.get("altTitleOrig", "")))
	altTitle.add_subfield(Subfield("b", data.get("altTitleTrans", "")))
	rec.add_field(altTitle)
	
	volume = Field("246", "3", "0")
	volume.add_subfield(Subfield("n", data.get("volumeNo", "")))
	volume.add_subfield(Subfield("p", data.get("volumeName", "")))
	rec.add_field(volume)
	
	for i in data.get("publisher", "").split("\n"):
		publisher = Field("260")
		publisher.add_subfield(Subfield("b", i))
		rec.add_field(publisher)
		
	extent = Field("300")
	extent.add_subfield(Subfield("a", data.get("extent", "").replace("\n", " ")))
	rec.add_field(extent)
	
	fileFormats = Field("340")
	fileFormats.add_subfield(Subfield("a", data.get("fileFormats", "").replace("\n", " ")))
	rec.add_field(fileFormats)
		
	for i in data.get("citation", "").split("\n"):
		citation = Field("500")
		citation.add_subfield(Subfield("a", i))
		rec.add_field(citation)
		
	degree = Field("502")
	degree.add_subfield(Subfield("a", data.get("degreeDefDate", "")))
	degree.add_subfield(Subfield("b", data.get("degreeName", "")))
	degree.add_subfield(Subfield("c", data.get("degreeGrantor", "")))
	degree.add_subfield(Subfield("g", data.get("degreeLevel", "")))
	rec.add_field(degree)
	
	restriction = Field("506")
	restriction.add_subfield(Subfield("a", data.get("restriction", "")))
	rec.add_field(restriction)
	
	if data.has_key("abstractCz"):
		abstractCz = Field("520")
		abstractCz.add_subfield(Subfield("a", data["abstractCz"].replace("\n", " ")))
		abstractCz.add_subfield(Subfield("9", "cze"))
		rec.add_field(abstractCz)
	
	if data.has_key("abstractEn"):
		abstractEn = Field("520")
		abstractEn.add_subfield(Subfield("a", data["abstractEn"].replace("\n", " ")))
		abstractEn.add_subfield(Subfield("9", "eng"))
		rec.add_field(abstractEn)
	
	licenceDefault = Field("540")
	licenceDefault.add_subfield(Subfield("a", data.get("licenceDefault", "")))
	rec.add_field(licenceDefault)
	
	licence = Field("540")
	licence.add_subfield(Subfield("a", data.get("licence", "")))
	rec.add_field(licence)
	
	note = Field("598")
	note.add_subfield(Subfield("a", data.get("note", "").replace("\n", " ")))
	rec.add_field(note)
	
	for i in data.get("selectedSubjects", "").split("\n"):
		psh = Field("650", ind2="7")
		psh.add_subfield(Subfield("a", i))
		psh.add_subfield(Subfield("2", "PSH"))
		rec.add_field(psh)
		
	for i in data.get("keywordsCz", "").split("\n"):
		keycz = Field("653")
		keycz.add_subfield(Subfield("a", i))
		rec.add_field(keycz)
		
	for i in data.get("keywordsEn", "").split("\n"):
		keyen = Field("653", "0")
		keyen.add_subfield(Subfield("a", i))
		rec.add_field(keyen)
		
	if data.has_key("degreeDisc"):
		degreeDisc = Field("656", ind2="7")
		degreeDisc.add_subfield(Subfield("a", data.get("degreeDisc", "")))
		degreeDisc.add_subfield(Subfield("2", "AKVO"))
		rec.add_field(degreeDisc)
	
	conference = Field("711", "2")
	conference.add_subfield(Subfield("a", data.get("confName", "")))
	conference.add_subfield(Subfield("g", data.get("confNameTrans", "")))
	conference.add_subfield(Subfield("c", data.get("confPlace", "")))
	conference.add_subfield(Subfield("d", data.get("confDate", "")))
	rec.add_field(conference)
	
	for i in data.get("authors", "").split("\n") + data.get("author", "").split("\n"):
		author = Field("720")
		author.add_subfield(Subfield("a", i))
		rec.add_field(author)
	
	if data.has_key("contribAdvisor"):	
		conAdv = Field("720")
		conAdv.add_subfield(Subfield("i", data["contribAdvisor"]))
		conAdv.add_subfield(Subfield("9", "advisor"))
		rec.add_field(conAdv)
	
	if data.has_key("contribOponent"):
		conOpo = Field("720")
		conOpo.add_subfield(Subfield("i", data["contribOponent"]))
		conOpo.add_subfield(Subfield("9", "oponent"))
		rec.add_field(conOpo)
	
	if data.has_key("contribOther"):	
		conOth = Field("720")
		conOth.add_subfield(Subfield("i", data["contribOther"]))
		conOth.add_subfield(Subfield("9", data.get("contribRole", "")))
		rec.add_field(conOth)
	
	submitter = Field("856", "0")
	submitter.add_subfield(Subfield("f", data.get("SuE", "")))
	rec.add_field(submitter)
	
	for i in xrange(1,6):
		if data.has_key("file%s_RENAMED"%i):
			sfile = Field("856", "4")
			sfile.add_subfield(Subfield("u", os.path.join(CFG_SITE_URL, "record", data["SN"], "files", data["file%s_RENAMED"%i])))
			sfile.add_subfield(Subfield("z", data.get("filedesc%s"%i, "")))
			rec.add_field(sfile)
		
	collection = Field("980")
	collection.add_subfield(Subfield("a", data["combo"+data["doctype"]]))
	rec.add_field(collection)
	
	institution = Field("998")
	institution.add_subfield(Subfield("a", data.get("institution", "")))
	rec.add_field(institution)
	
	project = Field("999", "C", "1")
	project.add_subfield(Subfield("a", data.get("projectId", "")))
	project.add_subfield(Subfield("b", data.get("projectProv", "")))
	rec.add_field(project)
	
	return rec.to_xml()
